﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;
using Meqs.Logic;

public partial class School_Report_SelectScoreEducation : System.Web.UI.Page
{
    protected int currPage = 0;
    protected List<SchoolStuReport> viewData = new List<SchoolStuReport>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind(SchoolCookie.ExamID, CommonScore.GetGradeStoreTable(SchoolCookie.ExamGrade), ">=", 600, "");
    }

    private void Bind(int examId, string scoreName, string sign, int indexNum, string subjectCondition)
    {
        ISchoolStuReportLogic logic = Container<ISchoolStuReportLogic>.Instance;
        List<SchoolStuReport> list = (List<SchoolStuReport>)logic.SelectStuEduScoreReport(examId, CommonScore.GetSchoolMark, sign, indexNum, subjectCondition, scoreName);
        viewData = list;
        int i = -1;
        double s = -1;
        foreach (SchoolStuReport ssr in viewData)
        {
            if (s != ssr.Score)
            {
                i = ssr.RowIndex;
                s = ssr.Score;
            }
            else
            {
                ssr.RowIndex = i;
            }
        }

        if (!sign.Contains(">"))
        {
            //int i = -1;
            //double s = -1;
            //list = new List<SchoolStuReport>();
            //for (int index = viewData.Count - 1; index >= 0; index--)
            //{
            //    if (s != viewData[index].Score)
            //    {
            //        i = viewData[index].RowIndex;
            //        s = viewData[index].Score;
            //    }
            //    else
            //    {
            //        viewData[index].RowIndex = i;
            //    }
            //    list.Insert(0, viewData[index]);
            //}
            //viewData = list;
            ListSort<SchoolStuReport> sort = new ListSort<SchoolStuReport>(viewData, "RowIndex", false);
            viewData = (List<SchoolStuReport>)sort.Sort();
        }

        //repeat.DataSource = list;
        //repeat.DataBind();
        AspNetPager1.RecordCount = viewData.Count;
        bindData(viewData, 1);
    }

    protected void btnSelect_Click(object sender, EventArgs e)
    {
        string sign = "", s = "", scoreName = "";
        int examId = 0, indexNum = 0, subjectId = 0;

        sign = txtSign.Text;
        s = txtIndexNum.Text;
        if (!String.IsNullOrEmpty(s))
        {
            if (int.TryParse(s, out indexNum))
                indexNum = Convert.ToInt32(s);
        }
        s = txtScore.Text;
        if (!String.IsNullOrEmpty(s))
        {
            if (int.TryParse(s, out subjectId))
                subjectId = Convert.ToInt32(s);
        }
        examId = SchoolCookie.ExamID;
        scoreName = CommonScore.GetGradeStoreTable(SchoolCookie.ExamGrade);

        string subjectCondition = "";
        if (subjectId > 0)
            subjectCondition = "and subjectId =" + subjectId;

        if (examId > 0)
            Bind(examId, scoreName, sign, indexNum, subjectCondition);

    }

    /**
    * 分页功能开始
    * */
    void bindData(List<SchoolStuReport> data, int pageindex)
    {
        if (pageindex > 0)
        {
            List<SchoolStuReport> orders = GetTeachers(data, (pageindex - 1) * 15, pageindex * AspNetPager1.PageSize - 1);
            if (data.Count <= AspNetPager1.PageSize)//若是绑定数据行数小于等于一页数据行数 怎不显示分页控件
                AspNetPager1.Visible = false;
            else
                AspNetPager1.Visible = true;
            repeat.DataSource = orders;
            repeat.DataBind();
            AspNetPager1.CurrentPageIndex = pageindex;
        }
    }

    /// <summary>
    /// 取分页数据
    /// </summary>
    /// <param name="data">总数据</param>
    /// <param name="start">开始的条数</param>
    /// <param name="len">取的条数</param>
    /// <returns></returns>
    private List<SchoolStuReport> GetTeachers(List<SchoolStuReport> data, int start, int len)
    {
        List<SchoolStuReport> allorders = new List<SchoolStuReport>();
        if (data.Count > 0)
        {
            for (int i = start; i <= len; i++)
            {
                if (data.Count > i)
                    allorders.Add(data[i]);
            }
        }
        return allorders;
    }
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        Wuqi.Webdiyer.PageChangedEventArgs pc = e as Wuqi.Webdiyer.PageChangedEventArgs;
        currPage = pc.NewPageIndex;
        btnSelect_Click(null, null);
        bindData(viewData, pc.NewPageIndex);
    }
    /**
    * 分页功能结束
    * */
}
